package com.siwei.apply.controller;

import com.siwei.apply.domain.NodeAttachment;
import com.siwei.apply.service.NodeAttachmentService;
import com.siwei.common.core.domain.R;
import com.siwei.common.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 节点附件控制器
 */
@RestController
@RequestMapping("/nodeattachment")
public class NodeAttachmentController extends BaseController {

    @Autowired
    private NodeAttachmentService nodeAttachmentService;

    /**
     * 处理文件并保存附件信息
     *
     * @param requestBody 包含filePath的请求体
     * @return 数据库存储后的id
     */
    @PostMapping("/process")
    public R<Map<String, String>> processFile(@RequestBody Map<String, String> requestBody) {
        try {
            String filePath = requestBody.get("filePath");

            if (filePath == null || filePath.trim().isEmpty()) {
                return R.fail("filePath不能为空");
            }

            String id = nodeAttachmentService.processFileAndSave(filePath);

            if (id != null) {
                Map<String, String> responseData = new HashMap<>();
                responseData.put("nodeAttachmentId", id);
                return R.ok(responseData);
            } else {
                return R.fail("处理文件失败");
            }

        } catch (Exception e) {
            logger.error("处理文件异常", e);
            return R.fail("处理文件异常：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询附件信息
     *
     * @param nodeAttachmentId 附件ID
     * @return 附件信息
     */
    @GetMapping("/nodeAttachmentId/{nodeAttachmentId}")
    public R<NodeAttachment> getById(@PathVariable String nodeAttachmentId) {
        try {
            if (nodeAttachmentId == null || nodeAttachmentId.trim().isEmpty()) {
                return R.fail("id不能为空");
            }

            NodeAttachment nodeAttachment = nodeAttachmentService.getById(nodeAttachmentId);
            return R.ok(nodeAttachment);

        } catch (Exception e) {
            logger.error("查询附件信息异常", e);
            return R.fail("查询附件信息异常：" + e.getMessage());
        }
    }

    /**
     * 根据节点ID查询附件信息
     *
     * @param nodeId 节点ID
     * @return 附件信息
     */
    @GetMapping("/node/{nodeId}")
    public R<NodeAttachment> getByNodeId(@PathVariable String nodeId) {
        try {
            if (nodeId == null || nodeId.trim().isEmpty()) {
                return R.fail("nodeId不能为空");
            }

            NodeAttachment nodeAttachment = nodeAttachmentService.getByNodeId(nodeId);
            return R.ok(nodeAttachment);

        } catch (Exception e) {
            logger.error("查询节点附件信息异常", e);
            return R.fail("查询节点附件信息异常：" + e.getMessage());
        }
    }

    /**
     * 根据节点ID删除附件信息
     *
     * @param nodeId 节点ID
     * @return 删除结果
     */
    @DeleteMapping("/{nodeId}")
    public R<Map<String, Object>> deleteByNodeId(@PathVariable String nodeId) {
        try {
            if (nodeId == null || nodeId.trim().isEmpty()) {
                return R.fail("nodeId不能为空");
            }

            nodeAttachmentService.deleteByNodeId(nodeId);

            Map<String, Object> responseData = new java.util.HashMap<>();
            responseData.put("nodeId", nodeId);
            responseData.put("success", true);

            return R.ok(responseData);

        } catch (Exception e) {
            logger.error("删除节点附件信息异常", e);
            return R.fail("删除节点附件信息异常：" + e.getMessage());
        }
    }

    /**
     * 更新附件记录的nodeId
     *
     * @param requestBody 包含nodeAttachmentId和nodeId的请求体
     * @return 更新结果
     */
    @PutMapping("/updateNodeId")
    public R<Map<String, Object>> updateNodeId(@RequestBody Map<String, String> requestBody) {
        try {
            String nodeAttachmentId = requestBody.get("nodeAttachmentId");
            String nodeId = requestBody.get("nodeId");

            if (nodeAttachmentId == null || nodeAttachmentId.trim().isEmpty()) {
                return R.fail("nodeAttachmentId不能为空");
            }

            if (nodeId == null || nodeId.trim().isEmpty()) {
                return R.fail("nodeId不能为空");
            }

            boolean success = nodeAttachmentService.updateNodeId(nodeAttachmentId, nodeId);

            Map<String, Object> responseData = new java.util.HashMap<>();
            responseData.put("nodeAttachmentId", nodeAttachmentId);
            responseData.put("nodeId", nodeId);
            responseData.put("success", success);

            if (success) {
                return R.ok(responseData);
            } else {
                return R.fail("更新nodeId失败");
            }

        } catch (Exception e) {
            logger.error("更新nodeId异常", e);
            return R.fail("更新nodeId异常：" + e.getMessage());
        }
    }
}
